const weiUtils = require('../../utils/util')
var amapFile = require('../../utils/amap-wx.130.js');
import moment from "moment";
Page({
  data: {
    lunarData:{},
    day:moment().date(),
    month: moment().month()+1,
    year:moment().year(),
    weather:{},
    show2: false,
    fieldValue: '',
    cascaderValue: '',
    minHour: 10,
    maxHour: 20,
    minDate: new Date(new Date().getFullYear() - 100, 1, 1).getTime(),
    maxDate: new Date(2019, 10, 1).getTime(),
    currentDate: new Date().getTime(),
  },
  onLoad(options) {
    let that = this;
    var myAmapFun = new amapFile.AMapWX({key:'bf32086032af1fa5fcac303fddf7e847'});
    myAmapFun.getWeather({
      success: function(data){
      	console.log('myAmapFundata:', data)
        data.liveData.weatherImgUrl = that.geticon(data.liveData.weather)
        console.log('data.liveData.weatherImgUrl:', data.liveData.weatherImgUrl)
        that.setData({
          weather: data
        });
        //成功回调
      },
      fail: function(info){
        //失败回调
        console.log('myAmapFundatafail',info)
      }
    })
    // wx.authorize({
    //   scope: 'scope.userLocation',
    //   success: function () {
        
    //     console.log('scope.userLocation')
    //     // 用户授权成功
    //     wx.getLocation({
    //       type: 'wgs84',
    //       success: function (res) {
    //       	console.log('ressssssssss:', res)
    //         const latitude = res.latitude; // 纬度 
    //         const longitude = res.longitude; // 经度
    //         const speed = res.speed;
    //         const accuracy = res.accuracy;
  

        
    //         // 调用逆地理编码接口获取当前位置信息
    //         //getAddressInfo(latitude, longitude)
    //         // 在这里调用获取天气信息的函数，并传入经纬度参数
    //         //getWeatherInfo(latitude, longitude);
    //       }
    //     })
    //   },
    //   fail: function () {
    //     // 用户拒绝授权或授权失败
    //     wx.showToast({
    //       title: '请授权地理位置信息',
    //       icon: 'none',
    //       duration: 2000
    //     });
    //   }
    // })
  },
  onReady() {

  },
  onShow() {
   this.initData()
  },
  onHide() {

  },
  onUnload() {

  },
  onShareAppMessage() {
    return {
      title: '',
    };
  },
  showPopup() {
    
    console.log('showPopup:')
    this.setData({ show2: true });
  },
  onClose() {
    this.setData({ show2: false });
  },
  onConfirm(e) {
  console.log('e:', e)
  let date = moment(e.detail).format("YYYY-MM-DD HH:mm")
    this.initData(date)
    this.setData({ 
      show2: false,
      day:moment(e.detail).date(),
      month: moment(e.detail).month()+1,
      year:moment(e.detail).year()
    });
  },
  getToday(){
    this.initData()
    this.setData({ 
      show2: false,
      day:moment().date(),
      month: moment().month()+1,
      year:moment().year(),
    });
  },
  getClassByElement(element) {
  console.log('element:', element)
    
    // console.log(element, element.includes('金'))
    if (element.includes('金')) {
      return '#f2c53f'; // 假设金色对应的背景色是黄色
    }
    if (element.includes('木')) {
      return '#228B22'; // 木色对应的背景色是绿色
    }
    if (element.includes('水')) {
      return '#00BFFF'; // 水色对应的背景色是蓝色
    }
    if (element.includes('火')) {
      return '#B22222'; // 火色对应的背景色是红色
    }
    if (element.includes('土')) {
      return '#A0522D'; // 土色对应的背景色是棕色
    }
    // return 'bg-gray-500'; // 默认背景色，如果没有匹配到任何一种
  },
  geticon(info) {
    console.log('info', info);
    if (info === '晴') {
      return 'https://zt-1304966785.cos.ap-nanjing.myqcloud.com/clear-day.svg';
    }
    if (info === '多云') {
      return 'https://zt-1304966785.cos.ap-nanjing.myqcloud.com/cloudy.svg';
    }
    if (info === '阴') {
      return 'https://zt-1304966785.cos.ap-nanjing.myqcloud.com/extreme.svg';
    }
    if (info.includes('雪')) {
      return 'https://zt-1304966785.cos.ap-nanjing.myqcloud.com/snow.svg';
    }
    if (info.includes('雨')) {
      return 'https://zt-1304966785.cos.ap-nanjing.myqcloud.com/rain.svg';
    }
    if (info === '9999') {
      return 'https://ztapp-1304966785.cos.ap-nanjing.myqcloud.com/svg/code-green.svg';
    }
  },
  initData(date) {
    if(!date)
    {
      date=moment().format("YYYY-MM-DD HH:mm");
    }
    let that = this;
    weiUtils.request({
      url: `https://zhangteng.cloud/5001/api/Rss/GetDate?dete=${date}`,
      success(data) {
      console.log('data2:', data)
        data.dao = data.dao.split('年')[0].replace('道歷','')
        data.fo = data.fo.split('年')[0]
        data.lunarDate1 = data.lunarDate.split('年')[1]
        data.lunarDate = data.lunarDate.split('年')[0]
        data.ganZhiArr = data.ganZhi.split('-')
        data.shengXiaoArr = data.shengXiao.split('-')
        data.naYinArr = data.naYin.split('-')
        data.naYinColorArr = [];
        data.naYinArr.forEach(element => {
          data.naYinColorArr.push(that.getClassByElement(element))
        });
      that.setData({
        lunarData: data
        });
       console.log(`request获取的数据2: `, that.data.lunarData)
      }
    });
  }
});